package com.da.nio;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class NIOFileChannel03 {
    public static void main(String[] args) throws Exception {
        FileInputStream inputStream = new FileInputStream("1.txt");
        FileChannel in = inputStream.getChannel();
        FileOutputStream outputStream = new FileOutputStream("2.txt");
        FileChannel out = outputStream.getChannel();

        ByteBuffer buffer = ByteBuffer.allocate(5);

        //循环读取
        while (true) {
            //这里有一个重要的操作，一定不要忘了清空buffer
            buffer.clear();
            int count = in.read(buffer);
            System.out.println("count = " + count);
            if (count == -1) {
                break;
            }
            //将buffer 中的数据写入到 fileChannel02 -- 2.txt
            buffer.flip();
            out.write(buffer);
        }

        //关闭相关的流
        inputStream.close();
        outputStream.close();
    }
}
